package com.jojo.android.zxlib.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 创建者 zx
 * 创建时间 2020/12/1 17:17
 * <p>
 * 没有数据展示空视图
 */
public class EmptyRecyclerView extends RecyclerView {
    private final String TAG = "EmptyRecyclerView";
    private LinearLayoutManager mLinearLayoutManager;
    private View loadingProgress;
    private View noMoreTips; //没有更多
    private boolean isHasMore = true;//是否还有更多
    private boolean isLoading;//是否加载中
    private View emptyView;
    private OnLoadMoreListener onLoadMoreListener;
    private OnAdapterDataUpdate onAdapterDataUpdate;
    private boolean enabledLoadMore = true;


    public EmptyRecyclerView(@NonNull Context context) {
        this(context, null);
    }

    public EmptyRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EmptyRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }


    //使布局管理器设置无效
    @Override
    public void setLayoutManager(@Nullable LayoutManager layout) {
//        super.setLayoutManager(layout);
    }

    public void setLayoutManager(@Nullable LinearLayoutManager layout) {
        super.setLayoutManager(layout);
    }

    private void init() {
        setLayoutManager(mLinearLayoutManager = new LinearLayoutManager(getContext(),
                LinearLayoutManager.VERTICAL, false));

    }

    public LinearLayoutManager getLinearLayoutManager() {
        return mLinearLayoutManager;
    }


    @Override
    public void onScrollStateChanged(int state) {
        super.onScrollStateChanged(state);
        if (enabledLoadMore && getAdapter() != null && mLinearLayoutManager != null) {
            if (mLinearLayoutManager.findLastVisibleItemPosition() >= getAdapter().getItemCount() - 1
                    && state == RecyclerView.SCROLL_STATE_IDLE && isHasMore && !isLoading) {
                if (loadingProgress != null)
                    loadingProgress.setVisibility(View.VISIBLE);
                isLoading = true;
                if (onLoadMoreListener != null) {
                    onLoadMoreListener.onLoadMore(EmptyRecyclerView.this);
                }
            }
        }
    }

    final private AdapterDataObserver observer = new AdapterDataObserver() {
        @Override
        public void onChanged() {
            checkIfEmpty();
            if (onAdapterDataUpdate != null) {
                onAdapterDataUpdate.onAdapterDataChanged();
            }
        }

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            checkIfEmpty();
            if (onAdapterDataUpdate != null) {
                onAdapterDataUpdate.onAdapterDataItemRangeInserted(positionStart, itemCount);
            }

        }

        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            checkIfEmpty();
            if (onAdapterDataUpdate != null) {
                onAdapterDataUpdate.onAdapterDataItemRangeRemoved(positionStart, itemCount);
            }
        }

        @Override
        public void onItemRangeChanged(int positionStart, int itemCount) {
            if (onAdapterDataUpdate!=null){
                onAdapterDataUpdate.onAdapterDataItemChanged(positionStart, itemCount);
            }
        }

    };


    private void checkIfEmpty() {
        if (emptyView != null && getAdapter() != null) {
            final boolean emptyViewVisible =
                    getAdapter().getItemCount() == 0;
            emptyView.setVisibility(emptyViewVisible ? VISIBLE : GONE);
            setVisibility(emptyViewVisible ? GONE : VISIBLE);
        }
    }

    @Override
    public void setAdapter(Adapter adapter) {
        final Adapter oldAdapter = getAdapter();
        if (oldAdapter != null) {
            oldAdapter.unregisterAdapterDataObserver(observer);
        }
        super.setAdapter(adapter);
        if (adapter != null) {
            adapter.registerAdapterDataObserver(observer);
        }

        checkIfEmpty();
    }

    //设置没有内容时，提示用户的空布局
    public void setEmptyView(View emptyView) {
        this.emptyView = emptyView;
        checkIfEmpty();
    }

    public void setEmptyViewById(@IdRes int id) {
        if (getContext() instanceof Activity) {
            emptyView = ((Activity) getContext()).findViewById(id);
        }
        checkIfEmpty();
    }

    public View getEmptyView() {
        return emptyView;
    }

    public View getLoadingProgress() {
        return loadingProgress;
    }

    public void setLoadingProgress(View loadingProgress) {
        this.loadingProgress = loadingProgress;
    }

    public View getNoMoreTips() {
        return noMoreTips;
    }

    public void setNoMoreTips(View noMoreTips) {
        this.noMoreTips = noMoreTips;
    }

    public boolean isHasMore() {
        return isHasMore;
    }

    public void setHasMore(boolean hasMore) {
        if (noMoreTips != null) {
            if (hasMore) {
                noMoreTips.setVisibility(GONE);
            } else {
                noMoreTips.setVisibility(VISIBLE);
            }
        }
        isHasMore = hasMore;
    }

    public boolean isLoading() {
        return isLoading;
    }

    public void setLoading(boolean loading) {
        if (loadingProgress != null) {
            if (loading) {
                loadingProgress.setVisibility(VISIBLE);
            } else {
                loadingProgress.setVisibility(GONE);
            }
        }
        isLoading = loading;
    }

    public boolean isEnabledLoadMore() {
        return enabledLoadMore;
    }

    public void setEnabledLoadMore(boolean enabledLoadMore) {
        this.enabledLoadMore = enabledLoadMore;
    }


    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }

    public void setOnAdapterDataUpdate(OnAdapterDataUpdate onAdapterDataUpdate) {
        this.onAdapterDataUpdate = onAdapterDataUpdate;
    }


    public interface OnLoadMoreListener {
        void onLoadMore(EmptyRecyclerView recyclerView);
    }

    public interface OnAdapterDataUpdate {
        void onAdapterDataChanged();

        void onAdapterDataItemRangeInserted(int positionStart, int itemCount);

        void onAdapterDataItemRangeRemoved(int positionStart, int itemCount);

        void onAdapterDataItemChanged(int positionStart, int itemCount);
    }
}

